package o;

import android.os.Build.VERSION;
import android.util.Log;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.TrustAnchor;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.X509TrustManager;

class fws extends fwy {
    private final Class<?> ˊ;
    private final fwu<Socket> ˋ;
    private final fwu<Socket> ˎ;
    private final fwu<Socket> ˏ;
    private final fwu<Socket> ॱ;
    private final c ᐝ = c.ॱ();

    static final class a implements fxb {
        private final X509TrustManager ˊ;
        private final Method ˋ;

        a(X509TrustManager x509TrustManager, Method method) {
            this.ˋ = method;
            this.ˊ = x509TrustManager;
        }

        public X509Certificate ˏ(X509Certificate x509Certificate) {
            try {
                TrustAnchor trustAnchor = (TrustAnchor) this.ˋ.invoke(this.ˊ, new Object[]{x509Certificate});
                if (trustAnchor != null) {
                    return trustAnchor.getTrustedCert();
                }
                return null;
            } catch (Exception e) {
                throw fvg.ॱ("unable to get issues and signature", e);
            } catch (InvocationTargetException e2) {
                return null;
            }
        }

        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (!(obj instanceof a)) {
                return false;
            }
            a aVar = (a) obj;
            if (this.ˊ.equals(aVar.ˊ) && this.ˋ.equals(aVar.ˋ)) {
                return true;
            }
            return false;
        }

        public int hashCode() {
            return this.ˊ.hashCode() + (this.ˋ.hashCode() * 31);
        }
    }

    static final class b extends fxe {
        private final Method ˊ;
        private final Object ॱ;

        b(Object obj, Method method) {
            this.ॱ = obj;
            this.ˊ = method;
        }

        public List<Certificate> ॱ(List<Certificate> list, String str) throws SSLPeerUnverifiedException {
            try {
                X509Certificate[] x509CertificateArr = (X509Certificate[]) list.toArray(new X509Certificate[list.size()]);
                return (List) this.ˊ.invoke(this.ॱ, new Object[]{x509CertificateArr, "RSA", str});
            } catch (Throwable e) {
                SSLPeerUnverifiedException sSLPeerUnverifiedException = new SSLPeerUnverifiedException(e.getMessage());
                sSLPeerUnverifiedException.initCause(e);
                throw sSLPeerUnverifiedException;
            } catch (IllegalAccessException e2) {
                throw new AssertionError(e2);
            }
        }

        public boolean equals(Object obj) {
            return obj instanceof b;
        }

        public int hashCode() {
            return 0;
        }
    }

    static final class c {
        private final Method ˊ;
        private final Method ˋ;
        private final Method ॱ;

        c(Method method, Method method2, Method method3) {
            this.ˋ = method;
            this.ˊ = method2;
            this.ॱ = method3;
        }

        Object ˎ(String str) {
            if (this.ˋ != null) {
                try {
                    Object invoke = this.ˋ.invoke(null, new Object[0]);
                    this.ˊ.invoke(invoke, new Object[]{str});
                    return invoke;
                } catch (Exception e) {
                }
            }
            return null;
        }

        boolean ˊ(Object obj) {
            if (obj == null) {
                return false;
            }
            try {
                this.ॱ.invoke(obj, new Object[0]);
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        static c ॱ() {
            Method method;
            Method method2;
            Method method3 = null;
            try {
                Class cls = Class.forName("dalvik.system.CloseGuard");
                method = cls.getMethod("get", new Class[0]);
                method2 = cls.getMethod("open", new Class[]{String.class});
                method3 = cls.getMethod("warnIfOpen", new Class[0]);
            } catch (Exception e) {
                method2 = method3;
                method = method3;
            }
            return new c(method, method2, method3);
        }
    }

    fws(Class<?> cls, fwu<Socket> o_fwu_java_net_Socket, fwu<Socket> o_fwu_java_net_Socket2, fwu<Socket> o_fwu_java_net_Socket3, fwu<Socket> o_fwu_java_net_Socket4) {
        this.ˊ = cls;
        this.ॱ = o_fwu_java_net_Socket;
        this.ˎ = o_fwu_java_net_Socket2;
        this.ˏ = o_fwu_java_net_Socket3;
        this.ˋ = o_fwu_java_net_Socket4;
    }

    public void ˋ(Socket socket, InetSocketAddress inetSocketAddress, int i) throws IOException {
        IOException iOException;
        try {
            socket.connect(inetSocketAddress, i);
        } catch (AssertionError e) {
            if (fvg.ˎ(e)) {
                throw new IOException(e);
            }
            throw e;
        } catch (Throwable e2) {
            iOException = new IOException("Exception in connect");
            iOException.initCause(e2);
            throw iOException;
        } catch (Throwable e22) {
            if (VERSION.SDK_INT == 26) {
                iOException = new IOException("Exception in connect");
                iOException.initCause(e22);
                throw iOException;
            }
            throw e22;
        }
    }

    public void ॱ(SSLSocket sSLSocket, String str, List<fut> list) {
        if (str != null) {
            this.ॱ.ˏ(sSLSocket, Boolean.valueOf(true));
            this.ˎ.ˏ(sSLSocket, str);
        }
        if (this.ˋ != null && this.ˋ.ˏ(sSLSocket)) {
            this.ˋ.ॱ(sSLSocket, fwy.ॱ(list));
        }
    }

    @Nullable
    public String ˊ(SSLSocket sSLSocket) {
        if (this.ˏ == null || !this.ˏ.ˏ(sSLSocket)) {
            return null;
        }
        byte[] bArr = (byte[]) this.ˏ.ॱ(sSLSocket, new Object[0]);
        return bArr != null ? new String(bArr, fvg.ˎ) : null;
    }

    public void ˏ(int i, String str, @Nullable Throwable th) {
        int i2 = i == 5 ? 5 : 3;
        if (th != null) {
            str = str + '\n' + Log.getStackTraceString(th);
        }
        int i3 = 0;
        int length = str.length();
        while (i3 < length) {
            int min;
            int indexOf = str.indexOf(10, i3);
            if (indexOf == -1) {
                indexOf = length;
            }
            while (true) {
                min = Math.min(indexOf, i3 + 4000);
                Log.println(i2, "OkHttp", str.substring(i3, min));
                if (min >= indexOf) {
                    break;
                }
                i3 = min;
            }
            i3 = min + 1;
        }
    }

    public Object ˏ(String str) {
        return this.ᐝ.ˎ(str);
    }

    public void ˎ(String str, Object obj) {
        if (!this.ᐝ.ˊ(obj)) {
            ˏ(5, str, null);
        }
    }

    public boolean ˊ(String str) {
        Exception e;
        try {
            Class cls = Class.forName("android.security.NetworkSecurityPolicy");
            return ॱ(str, cls, cls.getMethod("getInstance", new Class[0]).invoke(null, new Object[0]));
        } catch (ClassNotFoundException e2) {
            return super.ˊ(str);
        } catch (NoSuchMethodException e3) {
            return super.ˊ(str);
        } catch (IllegalAccessException e4) {
            e = e4;
            throw fvg.ॱ("unable to determine cleartext support", e);
        } catch (IllegalArgumentException e5) {
            e = e5;
            throw fvg.ॱ("unable to determine cleartext support", e);
        } catch (InvocationTargetException e6) {
            e = e6;
            throw fvg.ॱ("unable to determine cleartext support", e);
        }
    }

    private boolean ॱ(String str, Class<?> cls, Object obj) throws InvocationTargetException, IllegalAccessException {
        try {
            return ((Boolean) cls.getMethod("isCleartextTrafficPermitted", new Class[]{String.class}).invoke(obj, new Object[]{str})).booleanValue();
        } catch (NoSuchMethodException e) {
            return ˊ(str, cls, obj);
        }
    }

    private boolean ˊ(String str, Class<?> cls, Object obj) throws InvocationTargetException, IllegalAccessException {
        try {
            return ((Boolean) cls.getMethod("isCleartextTrafficPermitted", new Class[0]).invoke(obj, new Object[0])).booleanValue();
        } catch (NoSuchMethodException e) {
            return super.ˊ(str);
        }
    }

    private static boolean ˏ() {
        if (Security.getProvider("GMSCore_OpenSSL") != null) {
            return true;
        }
        try {
            Class.forName("android.net.Network");
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    public fxe ˊ(X509TrustManager x509TrustManager) {
        try {
            Class cls = Class.forName("android.net.http.X509TrustManagerExtensions");
            return new b(cls.getConstructor(new Class[]{X509TrustManager.class}).newInstance(new Object[]{x509TrustManager}), cls.getMethod("checkServerTrusted", new Class[]{X509Certificate[].class, String.class, String.class}));
        } catch (Exception e) {
            return super.ˊ(x509TrustManager);
        }
    }

    public static fwy ˎ() {
        Class cls;
        try {
            cls = Class.forName("com.android.org.conscrypt.SSLParametersImpl");
        } catch (ClassNotFoundException e) {
            cls = Class.forName("org.apache.harmony.xnet.provider.jsse.SSLParametersImpl");
        }
        try {
            fwu o_fwu;
            fwu o_fwu2;
            fwu o_fwu3 = new fwu(null, "setUseSessionTickets", Boolean.TYPE);
            fwu o_fwu4 = new fwu(null, "setHostname", String.class);
            if (ˏ()) {
                o_fwu = new fwu(byte[].class, "getAlpnSelectedProtocol", new Class[0]);
                o_fwu2 = new fwu(null, "setAlpnProtocols", byte[].class);
            } else {
                o_fwu2 = null;
                o_fwu = null;
            }
            return new fws(cls, o_fwu3, o_fwu4, o_fwu, o_fwu2);
        } catch (ClassNotFoundException e2) {
            return null;
        }
    }

    public fxb ˋ(X509TrustManager x509TrustManager) {
        try {
            Method declaredMethod = x509TrustManager.getClass().getDeclaredMethod("findTrustAnchorByIssuerAndSignature", new Class[]{X509Certificate.class});
            declaredMethod.setAccessible(true);
            return new a(x509TrustManager, declaredMethod);
        } catch (NoSuchMethodException e) {
            return super.ˋ(x509TrustManager);
        }
    }

    public SSLContext ॱ() {
        SSLContext instance;
        Object obj = 1;
        try {
            if (VERSION.SDK_INT < 16 || VERSION.SDK_INT >= 22) {
                obj = null;
            }
        } catch (NoClassDefFoundError e) {
        }
        if (obj != null) {
            try {
                instance = SSLContext.getInstance("TLSv1.2");
            } catch (NoSuchAlgorithmException e2) {
            }
            return instance;
        }
        try {
            instance = SSLContext.getInstance("TLS");
            return instance;
        } catch (Throwable e3) {
            throw new IllegalStateException("No TLS provider", e3);
        }
    }
}
